package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Zan;
import cn.wolfcode.trip.base.service.IZanService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/Zans")
public class ZanController {
    @Autowired
    private IZanService zanService;

    /**
     * 查询zan数量
     *
     * @param zan
     * @return
     */
    @GetMapping("/count")
    public int getZanNumByTravelId(Zan zan) {
        Integer num = zanService.getZanNumByTravelId(zan);
        return num;
    }

    @PostMapping("/saveOrDelete")
    public JsonResult getsaveOrDelete(Zan zan) {
        JsonResult jsonResult = new JsonResult();
        zan.setUserId(UserContext.getCurrentUser().getId());
        List<Zan> list = zanService.getListByUserId(zan);
        if (list != null && list.size() > 0) {
            //如果找到了这条记录,则删除该记录
            Zan zan1 = list.get(0);
            zanService.delete(zan1.getId());
            jsonResult.mark("取消赞");

        } else {
            //如果没有找到这条记录,则添加该记录
            zan.setStatus(true);
            zanService.save(zan);
        }
        return jsonResult;
    }

    @GetMapping("/zan")
    public JsonResult getZanColor(Zan zan) {
        JsonResult jsonResult = new JsonResult();
        List<Zan> list = zanService.getListByUserId(zan);
        if (list == null || list.size() == 0) {
            jsonResult.mark("没有赞哦");
        }
        return jsonResult;
    }

    @GetMapping("/zanTrueorFalse")
    public JsonResult getZanStatu(Zan zan) {
        JsonResult jsonResult = new JsonResult();
        int a = zanService.getZanByCommentId(zan);
        if (a == 0) {
            jsonResult.mark("没有赞");
        }
        return jsonResult;
    }

    @GetMapping("/zanStatus")
    public List<Zan> getZanStatus(Zan zan) {
        List<Zan> list = zanService.getListByUserId(zan);
        return list;
    }
}
